// miniprogram/pages/index/admin/review_speech/review_speech.js
const db = wx.cloud.database()
const _=db.command;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start_speeches:[],
    btn: true
  },
  cpnyNote: function(event){
    
    const speech_note=event.detail.value
    const sindex=event.target.dataset.sindex;
    this.data.start_speeches[sindex].speech_obj.speech_note=speech_note;
  },
  check: function(event){
    const that = this;
    let tem="";
    wx.requestSubscribeMessage({
      tmplIds: ['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'], 
      success: res=>{
        tem = res['iM5myfGg-CJVqdMbdArIBeH1-mrLYlnjspTwLvFtaZo'];
        if(tem=="accept"){
            wx.showToast({
            title: '已开启',
          })
          
          if(event.target.dataset.t=="yes"){
             that.onYes(event);
          }else{
            that.onNo(event);
          }
          
         
        }else{
          wx.showToast({
            title: '已拒绝',
          })
        }
      },
      fail: res=>{
          wx.showToast({
            title: '已拒绝',
          })
      }
    })
  },
  onYes: function(event){
    this.setData({btn:true})
    //改变obj_type为0,从数组中移除该元素
    const that=this;
    const sindex=event.target.dataset.sindex;
     
    const speech_note= this.data.start_speeches[sindex].speech_obj.speech_note;
    const _id=this.data.start_speeches[sindex]._id;
  
    
    db.collection("speech").doc(_id).update({
      data: {
        'speech_obj.speech_note': speech_note,
        'speech_obj.speech_type': 0
      }
    }).then(res=>{
      wx.cloud.callFunction({
        name: 'notice',
        data: {
          target: that.data.start_speeches[sindex].speech_obj.openid,
          title: '申请成功',
          content: that.data.start_speeches[sindex].speech_obj.speech_name,
          note: speech_note
        }
      })

      let start_speeches=that.data.start_speeches;
      start_speeches.splice(sindex,1);
       that.setData({
        start_speeches: start_speeches
       })
       
       getApp().tos("Yes pass the speech!");
    })
    this.setData({btn: false})
  },
  onNo: function(event){
    this.setData({btn: true})
    //从数据库移除元素
    //从数组移除元素
    const that=this;
    const sindex=event.target.dataset.sindex;
    const speech_note= this.data.start_speeches[sindex].speech_obj.speech_note;
    let start_speeches=that.data.start_speeches;
    
    db.collection("speech").doc(that.data.start_speeches[sindex]._id).remove().then(res=>{
      wx.cloud.callFunction({
        name: 'notice',
        data: {
          target: that.data.start_speeches[sindex].speech_obj.openid,
          title: '申请失败',
          content: that.data.start_speeches[sindex].speech_obj.speech_name,
          note: speech_note
        }
      })
      start_speeches.splice(sindex,1);
       that.setData({
        start_speeches: start_speeches
       })
      
       getApp().tos("no pass the speech!");
    })
    this.setData({btn: false})
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // const that=this;
    // db.collection("speech").where({
    //   'speech_obj.speech_type': -1
    // }).get().then(res=>{
    //   that.setData({start_speeches: res.data})
       
    // }) 
    this.loadData()
  },
  loadData: function(){
    const that=this;
    db.collection('speech')
   .aggregate()
   .match({'speech_obj.speech_type': -1})
   .sample({size: 10})
   .end().then(res=>{
    that.setData({start_speeches: res.list})
     
   })
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({btn: false})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
    this.loadData()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})